define([
    "UtilDir/util",
    "UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
    "Date","DateCN", "css!DateCss",
    "EasyUI","EasyUI-lang"
],function(Util, Tool, SelectOrg, SearchBlock){

    var getHddcCjTaskController = function(){
        return getServer() + "/hddc/hddcCjTasks";
    };
    var map=null;
    var projectTask=null;
    var lay1=null;
    var lay2=null;
    var lay3 =null;
    var lay4 =null;
    var lay5 =null;
    var lay6 =null;
    var replayDataInterval = null;
    function initMap(data) {
        projectTask=data;
        if(map==null) {
            //初始化地图对象

            /*
            var imageURL = "http://t0.tianditu.gov.cn/img_w/wmts?" +
                "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles" +
                "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=acce9f1241bb60d5164939ee53173caf";
             */
            var imageURL1="https://fxpc.mem.gov.cn/wmts?service=wmts&request=gettile&version=1.0.0&layer=vec&style=default&tilematrixset=c&format=tiles&tilematrix={z}&tilerow={y}&tilecol={x} ";
            lay1 = new T.TileLayer(imageURL1, {minZoom: 1, maxZoom: 18});
            var imageURL2="https://fxpc.mem.gov.cn/wmts?service=wmts&request=gettile&version=1.0.0&layer=cva&style=default&tilematrixset=c&format=tiles&tilematrix={z}&tilerow={y}&tilecol={x} ";
            lay2 = new T.TileLayer(imageURL2, {minZoom: 1, maxZoom: 18});
            var imageURL3="https://fxpc.mem.gov.cn/wmts?service=wmts&request=gettile&version=1.0.0&layer=img&style=default&tilematrixset=c&format=tiles&tilematrix={z}&tilerow={y}&tilecol={x} ";
            lay3 = new T.TileLayer(imageURL3, {minZoom: 1, maxZoom: 18});
            var imageURL4="https://fxpc.mem.gov.cn/wmts?service=wmts&request=gettile&version=1.0.0&layer=cia&style=default&tilematrixset=c&format=tiles&tilematrix={z}&tilerow={y}&tilecol={x} ";
            lay4 = new T.TileLayer(imageURL4, {minZoom: 1, maxZoom: 18});
            var imageURL5="https://fxpc.mem.gov.cn/wmts?service=wmts&request=gettile&version=1.0.0&layer=ter&style=default&tilematrixset=c&format=tiles&tilematrix={z}&tilerow={y}&tilecol={x} ";
            lay5 = new T.TileLayer(imageURL5, {minZoom: 1, maxZoom: 18});
            var imageURL6="https://fxpc.mem.gov.cn/wmts?service=wmts&request=gettile&version=1.0.0&layer=cat&style=default&tilematrixset=c&format=tiles&tilematrix={z}&tilerow={y}&tilecol={x} ";
            lay6 = new T.TileLayer(imageURL6, {minZoom: 1, maxZoom: 18});

            /*var imageURL5="https://fxpc.mem.gov.cn/wmts?service=WMTS&request=GetTile&version=1.0.0&layer=img08&style=default&tilematrixSet=c&format=tile&height=256&width=256&tilematrix={z}&tilerow={y}&tilecol={x} ";
            lay5 = new T.TileLayer(imageURL5, {minZoom: 1, maxZoom: 18});
            var imageURL6="https://fxpc.mem.gov.cn/wmts?service=WMTS&request=GetTile&version=1.0.0&layer=img08&style=default&tilematrixSet=c&format=tile&height=256&width=256&tilematrix={z}&tilerow={y}&tilecol={x} ";
            lay6 = new T.TileLayer(imageURL6, {minZoom: 1, maxZoom: 18});*/
            var config = {
                projection: "EPSG:4326",
                minZoom: 4,
                layers: [lay1,lay2]
            };
            //https://fxpc.mem.gov.cn/wmts?service=wmts&request=gettile&version=1.0.0&layer=cva&style=default&tilematrixset=c&format=tiles&tilematrix=9&tilerow=75&tilecol=423
            map = new T.Map("hddctask-map",config);
            map.clearOverLays();
            //设置显示地图的中心点和级别
            map.centerAndZoom(new T.LngLat(116.40969,39.89945),4);

            var control=new T.Control.Zoom();
            map.addControl(control);

            //创建比例尺控件对象
            var scale = new T.Control.Scale();
            //scale.setOptions()
            map.addControl(scale);

            map.enableScrollWheelZoom();
            map.enableDrag();

            //创建鹰眼控件对象
            var miniMap = new T.Control.OverviewMap({
                 isOpen: true,
                 size: new T.Point(150, 150)
             });
             map.addControl(miniMap);

            var bodyWidth = window.innerWidth-15;//1920px
            var mapHeight =  window.innerHeight-40;//796px
            var mapWidth = bodyWidth;//1469px

            $("#hddctask-mapDiv").css("height",mapHeight + "px");
            $("#hddctask-mapDiv").css("width",mapWidth + "px");
            $("#hddctask-map").css("height",mapHeight + "px");
            $("#hddctask-map").css("width",mapWidth + "px");

            map.checkResize();

            $("#maptype_displaybutton").css("z-index", "1100");
            $("#maptype_displaybutton").show();

        }
        map.clearOverLays();
        //initMapInterface();
        initTaskToolsEvent();
        initData(data);
    };

    function replaylonlat(lon,lat){
        this.lon=parseFloat(lon);
        this.lat=parseFloat(lat);
    }

    var replayDatas=[];
    function initData(data) {
        map.clearOverLays();
        replayDatas=[];
        replayDatas.length=0;
        var dataArrays=data.split(";");
        for(var i=0;i<dataArrays.length;i++){
            var lonlats=dataArrays[i].split(",");
            if(lonlats.length==2){
                var tdata=new replaylonlat(lonlats[0],lonlats[1]);
                replayDatas.push(tdata);
            }
        }
        if(replayDatas.length>0){
            var i = 0;
            replayDataInterval = setInterval(function () {
                i++;
                if (i < replayDatas.length) {
                    var p1 = new T.LngLat(replayDatas[i].lon, replayDatas[i].lat);
                    var p2 = new T.LngLat(replayDatas[i - 1].lon, replayDatas[i - 1].lat);
                    if (i == 1) {
                        var zoom = 16;
                        map.centerAndZoom(p2, zoom);
                        var icon = new T.Icon({
                            iconUrl: "/hdc/static/app/hddc/hddccjtask/images/start.png",
                            iconSize: new T.Point(19, 27),
                            iconAnchor: new T.Point(10, 25)
                        });
                        var marker = new T.Marker(p2, {icon: icon});
                        map.addOverLay(marker);
                    } else if (i == replayDatas.length - 1) {
                        var icon = new T.Icon({
                            iconUrl: "/hdc/static/app/hddc/hddccjtask/images/end.png",
                            iconSize: new T.Point(19, 27),
                            iconAnchor: new T.Point(10, 25)
                        });
                        var marker = new T.Marker(p1, {icon: icon});
                        map.addOverLay(marker);
                    }
                    var lnglatArray = [];//[replayDatas[i].point,replayDatas[i-1].point];
                    lnglatArray.push(p1);
                    lnglatArray.push(p2);

                    var line = new T.Polyline(lnglatArray);
                    map.addOverLay(line);
                } else {
                    console.log(123);
                    clearInterval(replayDataInterval);
                    replayDataInterval=null;
                    Util.alert("回放结束。");
                    return;
                }

            }, 1000);
        }
    }

    function initClearMap(){
        if(replayDataInterval!=null){
            clearInterval(replayDataInterval);
            replayDataInterval=null;
        }
        map.clearOverLays();
    }

    var hitIndex=0;
    function initTaskToolsEvent(){
        $("#button-maptypeDisplayDiv").unbind("click");
        $("#button-maptypeDisplayDiv").click(function () {
            $("#maptype-maptypeDisplayDiv").css("z-index", "1100");
            
            if(hitIndex==0){
                $("#maptype-maptypeDisplayDiv").css("display", "block");
                //$("#maptype-maptypeDisplayDiv").css("color", "black");
                hitIndex=1;
            }else{
                $("#maptype-maptypeDisplayDiv").css("display", "none");
                //$("#maptype-maptypeDisplayDiv").css("color", "3B49EE");
                hitIndex=0;
            }
        });
        $("#maptype1-DisplayImage").unbind("click");
        $("#maptype1-DisplayImage").click(function () {
            hitIndex=0;
            map.removeLayer(lay3);
            map.removeLayer(lay4);
            map.removeLayer(lay5);
            map.removeLayer(lay6);
            $("#maptype-maptypeDisplayDiv").css("display", "none");
        });
        $("#maptype2-DisplayImage").unbind("click");
        $("#maptype2-DisplayImage").click(function () {
            hitIndex=0;
            map.removeLayer(lay5);
            map.removeLayer(lay6);
            map.addLayer(lay3);
            map.addLayer(lay4);
            $("#maptype-maptypeDisplayDiv").css("display", "none");
        });
        $("#maptype3-DisplayImage").unbind("click");
        $("#maptype3-DisplayImage").click(function () {
            hitIndex=0;
            map.removeLayer(lay3);
            map.removeLayer(lay4);
            map.addLayer(lay5);
            map.addLayer(lay6);
            $("#maptype-maptypeDisplayDiv").css("display", "none");
        });
    }

    return {
        initMap:initMap,
        initClearMap:initClearMap
    };
});